import tkinter as tk
import random
import sys


class TipApp:
    def __init__(self):
        self.batch_windows = []
        self.single_window = None

    def show_single_tip(self):
        """初始单个弹窗"""
        self.single_window = tk.Tk()
        self.single_window.title('专属提示')

        # 屏幕居中显示
        screen_width = self.single_window.winfo_screenwidth()
        screen_height = self.single_window.winfo_screenheight()
        window_width = 280
        window_height = 100
        x = (screen_width - window_width) // 2
        y = (screen_height - window_height) // 2
        self.single_window.geometry(f"{window_width}x{window_height}+{x}+{y}")

        # 初始提示语
        tip = "我喜欢你"
        bg = 'skyblue'

        tk.Label(
            self.single_window,
            text=tip,
            bg=bg,
            font=('Arial', 14),
            width=30,
            height=3
        ).pack()

        # 绑定空格键退出
        self.single_window.bind('<space>', self.on_space_global)
        self.single_window.attributes('-topmost', True)

        # 关闭单个弹窗后，启动批量弹窗
        self.single_window.protocol("WM_DELETE_WINDOW", self.start_batch_tips)

        self.single_window.mainloop()

    def create_batch_window(self, count):
        """创建批量弹窗的单个窗口"""
        if count <= 0:
            return

        window = tk.Toplevel()
        self.batch_windows.append(window)

        screen_width = window.winfo_screenwidth()
        screen_height = window.winfo_screenheight()

        window_width = 280
        window_height = 100
        x = random.randrange(0, screen_width - window_width)
        y = random.randrange(0, screen_height - window_height)

        window.title('温馨提示')
        window.geometry(f"{window_width}x{window_height}+{x}+{y}")

        tips = [
            '多喝水哦', '保持微笑', '每天都要元气满满',
            '记得吃水果', '保持好心情', '好好爱自己', '我想你了',
            '梦想成真', '期待下一次见面', '金榜题名',
            '顺顺利利', '早点休息', '愿所有烦恼都消失',
            '别熬夜', '今天过得开心嘛', '天冷了，多穿衣服', '晚安'
        ]
        tip = random.choice(tips)

        bg_colors = [
            'lightpink', 'skyblue', 'lightgreen', 'lavender',
            'lightyellow', 'plum', 'coral', 'bisque', 'aquamarine'
        ]
        bg = random.choice(bg_colors)

        tk.Label(
            window,
            text=tip,
            bg=bg,
            font=('Arial', 14),
            width=30,
            height=3
        ).pack()

        # 绑定空格键退出
        window.bind('<space>', self.on_space_global)
        window.attributes('-topmost', True)

        window.protocol("WM_DELETE_WINDOW", self.start_batch_tips)

        # 调度创建下一个窗口
        if count > 1:
            window.after(50, lambda: self.create_batch_window(count - 1))

    def start_batch_tips(self):
        """启动批量弹窗"""
        if self.single_window:
            self.single_window.destroy()
            self.single_window = None

        # 使用after方法在主线程中创建批量窗口
        self.create_batch_window(100)

    def on_space_global(self, event=None):
        """全局空格键退出处理"""
        # 关闭所有窗口
        if self.single_window:
            self.single_window.destroy()

        for window in self.batch_windows:
            try:
                window.destroy()
            except:
                pass

        # 退出程序
        sys.exit()


if __name__ == '__main__':
    app = TipApp()
    app.show_single_tip()
